Imports System.Collections.Generic
Imports Security.Business

Partial Class Provider_Users_Edit
    Inherits ECGPage

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not Page.IsPostBack Then
            If String.IsNullOrEmpty(Request.Params("id")) OrElse Request.Params("id") = Guid.Empty.ToString() Then
                dvUser.ChangeMode(DetailsViewMode.Insert)
            Else
                ShowRoles()
                ShowGroups()
            End If
        End If
    End Sub

    Private Sub ShowRoles()
        If Not String.IsNullOrEmpty(Request.Params("id")) Then
            grdUserRoles.DataSource = UserService.GetRoles(New Guid(Request.Params("id")), False)
            grdUserRoles.DataBind()
        End If
    End Sub

    Protected Sub btnAddRole_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnAddRole.Click
        If Not String.IsNullOrEmpty(Request.Params("id")) Then
            Try
                UserService.AddRole(New Guid(Request.Params("id")), txtNewRole.Text)
                ShowRoles()
            Catch ex As Exception
                HandleError(ex)
            End Try
        End If
    End Sub

    Protected Sub dvRole_ItemDeleting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DetailsViewDeleteEventArgs)
        e.Cancel = True
        If Not String.IsNullOrEmpty(Request.Params("id")) AndAlso Not grdUserRoles.SelectedValue Is Nothing Then
            Try
                UserService.RemoveRole(New Guid(Request.Params("id")), grdUserRoles.SelectedValue)
                ShowRoles()
            Catch ex As Exception
                HandleError(ex)
            End Try
        End If
    End Sub

    Protected Sub dvUser_ModeChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DetailsViewModeEventArgs)
        If e.NewMode = DetailsViewMode.Insert Then
            TabPanel2.Enabled = False
            TabPanel3.Enabled = False
        Else
            TabPanel2.Enabled = True
            TabPanel3.Enabled = True
        End If
    End Sub

    Private Sub ShowGroups()
        If Not String.IsNullOrEmpty(Request.Params("id")) Then
            grdUserGroups.DataSource = UserService.GetGroups(New Guid(Request.Params("id")))
            grdUserGroups.DataBind()
        End If
    End Sub

    Protected Sub dvGroup_ItemDeleting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DetailsViewDeleteEventArgs)
        e.Cancel = True
        If Not String.IsNullOrEmpty(Request.Params("id")) AndAlso Not grdUserRoles.SelectedValue Is Nothing Then
            Try
                UserService.RemoveRole(New Guid(Request.Params("id")), grdUserRoles.SelectedValue)
                ShowGroups()
            Catch ex As Exception
                HandleError(ex)
            End Try
        End If
    End Sub

    Protected Sub btnAddGroup_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        If Not String.IsNullOrEmpty(Request.Params("id")) Then
            Try
                UserService.AddGroup(New Guid(Request.Params("id")), txtNewGroup.Text)
                ShowGroups()
            Catch ex As Exception
                HandleError(ex)
            End Try
        End If
    End Sub
End Class
